<?php

{{ builderInfo.php|raw }}

set_time_limit(60*10);
session_start();

if (php_sapi_name() != 'cli') {
  exit('Installer can be launched only from CLI');
}

// load configs
require_once(__DIR__ . '/ConfigEnv.php');
require_once(__DIR__ . '/src/ConfigApp.php');

// load application class
require(__DIR__ . '/src/App.php');

try {

  $app = new {{ appNamespace }}($config, TRUE);
  $app->console->cliEchoEnabled = TRUE;

  $app->install();

  // user roles and users
  $userModel = new ($app->getCoreClass('Models\\User'))($app);
  $userRoleModel = new ($app->getCoreClass('Models\\UserRole'))($app);
  $userHasRoleModel = new ($app->getCoreClass('Models\\UserHasRole'))($app);

  $idRoleAdministrator = $userRoleModel->insertRow(['name' => 'Administrator']);

  $adminPassword = 'administrator';

  $idUserAdministrator = $userModel->insertRow([
    'login' => 'administrator',
    'password' => $adminPassword,
    'password_1' => $adminPassword,
    'password_2' => $adminPassword,
    'is_active' => 1,
  ]);

  $userHasRoleModel->insertRow([
    'id_user' => $idUserAdministrator,
    'id_role' => $idRoleAdministrator,
  ]);

  $errors = $app->console->getErrors();
  if (count($errors) > 0) {
    echo "ADIOS app installation failed. Following errors occured during the installation.\n";
    echo "\n";
    foreach ($errors as $error) {
      echo $error[0] . "\n";
      echo $error[1]['exception'] . "\n";
      echo "\n";
    }

    exit(1);
  }

  echo "ADIOS app installation was successful.\n";

} catch (\Exception $e) {
  echo $e->getMessage();
}
